package manager
{
	
	import flash.display.*;
	import flash.events.Event;
	import flash.text.TextField;
	
	import remoting.Server;
	import model.*;
	import log.Logger;
	
	/**
	 * -- GameMgr --
	 * @desc	game manager, in charge of game's main logic and flow
	 * @author	Aqua (built on 2011-11-08)
	 */
	public class GameMgr
	{
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * -- (getter) instance --
		 * @desc		get singleton of this class
		 * @return		singleton of this class
		 *************************************************************************/
		public static function get instance():GameMgr
		{
			if (null == _instance)
			{
				_instance = new GameMgr(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * -- singleton --
		 * @desc		a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * -- GameMgr --
		 * @desc		constructor
		 * @param		(s) a private function used to enforce singlton
		 *************************************************************************/
		public function GameMgr(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * -- (getter) localMode --
		 * @desc		get if game is running in local mode
		 * @return		if game is running in local mode
		 *************************************************************************/
		public function get localMode():Boolean
		{
			return true;
		}
		
		/**************************************************************************
		 * -- (getter) localUser --
		 * @desc		get locally defined user's id, ignore this while returning -1
		 * @return		uid
		 *************************************************************************/
		public function get localUser():int
		{
			return 2;
		}
		
		/**************************************************************************
		 * -- startUp --
		 * @desc		start to run game
		 * @param		(main) parent of all objects on stage
		 *************************************************************************/
		public function startUp(main:DisplayObject):void
		{
			Logger.log("** start up");
			_main = DisplayObjectContainer(main);
			_main.addEventListener(Event.ENTER_FRAME, loadResHandler);
			ResourceMgr.instance.loadRes();
		}
		
		
		//------------------//
		//     Privates     //
		//------------------//
		
		/**************************************************************************
		 * -- loadResHandler --
		 * @desc		check if resources are loaded by every frame
		 * @param		(e) event information
		 *************************************************************************/
		private function loadResHandler(e:Event):void
		{
			if (ResourceMgr.instance.loadComplete)
			{
				_main.removeEventListener(Event.ENTER_FRAME, loadResHandler);
				Server.connet(onConnected);
			}
			else
			{
				// update percent of loaded resources
			}
		}
		
		/**************************************************************************
		 * -- onConnected --
		 * @desc		callback when connected to server
		 *************************************************************************/
		private function onConnected():void
		{
			Logger.log("** server connected");
			ProxyMgr.instance.init();
			//SoundMgr.instance.init();
			ViewMgr.instance.init(_main);
			
			_userPx = ProxyMgr.instance.getProxy(UserPx.NAME) as UserPx;
			_charaPx = ProxyMgr.instance.getProxy(CharacterPx.NAME) as CharacterPx;
			
			_userPx.initUser();
			_main.addEventListener(Event.ENTER_FRAME, initUserHandler);
		}
		
		/**************************************************************************
		 * -- initUserHandler --
		 * @desc		check if user data is inited by every frame
		 * @param		(e) event information
		 *************************************************************************/
		private function initUserHandler(e:Event):void
		{
			if (_userPx.inited && _charaPx.inited)
			{
				_main.removeEventListener(Event.ENTER_FRAME, initUserHandler);
				ViewMgr.instance.startUp();
			}
		}
		
		
		private static var _instance:GameMgr;
		
		public const PATH_LOCAL	:String = "../resource/";
		public const PATH		:String = "resource/"
		
		private var _main		:DisplayObjectContainer;
		private var _userPx		:UserPx;
		private var _charaPx	:CharacterPx;
	}
	
}